package com.example.demo.web.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

/**
 * Created by kong on 2017/9/12.
 */
public class PdfMaker {

    private final static float IMAGE_X = 110F;
    private final static float IMAGE_Y = 500F;
    private final static float TABLE_BEFORE_SPACE = 10F;
    private final static float TABLE_AFTER_SPACE = 10F;

    public static void main(String[] args) throws Exception {
//        pdfMake1();
//        pdfMakeTable();
//        pdfMakeList();
//        pdfMakeStyle();
        pdfMakePwd();
    }

    /**
     * pdf插入文字和图片
     */
    public static void pdfMake1(){
        try {
            //1.新建Document对象
            Document document = new Document();
            //2.建立一个书写器与document对象关联，通过书写器可以将文档写入磁盘
            PdfWriter.getInstance(document, new FileOutputStream(new File("E:\\MyDownloads\\test.pdf")));
            //3.打开文档
            document.open();
            //4.添加一内容段落
            document.add(new Paragraph("hello world")); //添加内容

            Image image1 = Image.getInstance("E:\\workhistory\\springcloud\\UserProvider\\0.jpg");   //添加图片
            image1.setAbsolutePosition(IMAGE_X,IMAGE_Y); //设置图片的x轴和y轴
            image1.scaleAbsolute(200,200);
            document.add(image1);  //添加图片

            Image image2 = Image.getInstance("E:\\workhistory\\springcloud\\UserProvider\\1.jpg");
            document.add(image2);

            //5.关闭文档
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * pdf生成表格
     */
    public static void pdfMakeTable(){
        try {
            //1.新建Document对象
            Document document = new Document();

            PdfWriter.getInstance(document, new FileOutputStream(new File("E:\\MyDownloads\\test.pdf")));
            //3.打开文档
            document.open();
            //4.添加一内容段落
            document.add(new Paragraph("hello world")); //添加内容

            Image image1 = Image.getInstance("E:\\workhistory\\springcloud\\UserProvider\\0.jpg");   //添加图片
            image1.setAbsolutePosition(IMAGE_X,IMAGE_Y); //设置图片的x轴和y轴
            image1.scaleAbsolute(200,200);
            document.add(image1);  //添加图片

            PdfPTable pdfPTable = new PdfPTable(3);  //3列的表
            pdfPTable.setWidthPercentage(100);   //宽度填充
            pdfPTable.setSpacingBefore(TABLE_BEFORE_SPACE);  //前间距
            pdfPTable.setSpacingAfter(TABLE_AFTER_SPACE);    //后间距

            ArrayList<PdfPRow> rows = pdfPTable.getRows();
//            float[] columnWidths = {1f, 2f, 3f};  //设置列宽
//            pdfPTable.setWidths(columnWidths);
            //行1
            PdfPCell cells1[]= new PdfPCell[3];
            PdfPRow row1 = new PdfPRow(cells1);

            //单元格
            cells1[0] = new PdfPCell(new Paragraph("111"));//单元格内容
            cells1[0].setBorderColor(BaseColor.BLUE);//边框验证
            cells1[0].setPaddingLeft(20);//左填充20
            cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中

            cells1[1] = new PdfPCell(new Paragraph("222"));
            cells1[2] = new PdfPCell(new Paragraph("333"));

            //行2
            PdfPCell cells2[]= new PdfPCell[3];
            PdfPRow row2 = new PdfPRow(cells2);
//            cells2[0] = new PdfPCell(new Paragraph("444"));
            int i = 0;
            for(int q=0;q<3;q++){
                cells2[q] = new PdfPCell(new Paragraph("index"+q));
            }
            //把第一行添加到集合
            rows.add(row1);
            rows.add(row2);

            document.add(pdfPTable);


            //5.关闭文档
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * pdf生成列表
     */
    public static void pdfMakeList(){
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\MyDownloads\\test.pdf"));
            document.open();
            document.add(new Paragraph("dsfsdfaa",new Font(Font.FontFamily.COURIER,20,1,BaseColor.BLUE)));

            //添加有序列表
            List list = new List(List.ORDERED);
            list.add(new ListItem("Seq One"));
            list.add(new ListItem("Seq Two"));
            list.add(new ListItem("Seq Three"));

            document.add(list);

            document.close();
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

    /**
     * pdf格式化输出
     */
    public static void pdfMakeStyle(){
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,new FileOutputStream("E:\\MyDownloads\\test.pdf"));

            document.open();
            //解决中文不显示问题
            BaseFont baseChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseChinese);

            font.setColor(BaseColor.RED);  //颜色
            font.setSize(20);  //大小
            //段落文本
            Paragraph paragraph = new Paragraph("就是对方立刻房价极度分裂框架阿喀琉斯代价是六块腹肌卡了埃里克第三方就开了大家是否离开敬爱的索拉卡卡死了对方即可  这是一个段落文本！！！", font);
            Paragraph paragraph3 = new Paragraph("就是对方立刻房价极度分裂框架阿喀琉斯代价是六块腹肌卡了埃里克第三方就开了大家是否离开敬爱的索拉卡卡死了对方即可  这是一个段落文本！！！", font);
            document.add(paragraph);
            document.add(paragraph3);

            Font font1 = new Font(baseChinese);
            font1.setColor(BaseColor.BLUE);
            font1.setSize(20);
            //创建章节
            Paragraph chapterTitle = new Paragraph("段落标题", font1);
            Chapter chapter1 = new Chapter(chapterTitle,1);
            chapter1.setNumberDepth(0);

            Paragraph sectionTitle = new Paragraph("部分标题1", font1);
            Section section1 = chapter1.addSection(sectionTitle);

            Paragraph contents = new Paragraph("     部分内容1", font);
            chapter1.add(contents);

            Paragraph sectionTitle2 = new Paragraph("部分标题2", font1);
            Section section2 = chapter1.addSection(sectionTitle2);

            Paragraph contents2 = new Paragraph("     部分内容2", font);
            chapter1.add(contents2);

            document.add(chapter1);


            document.close();


        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * pdf加密
     */
    public static void pdfMakePwd(){
        try {
            //1.新建Document对象
            Document document = new Document();
            //2.建立一个书写器与document对象关联，通过书写器可以将文档写入磁盘
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(new File("E:\\MyDownloads\\test.pdf")));

            String userPwd = "123456";  //用户密码
            String ownerPwd = "wd"; //拥有者密码
            pdfWriter.setEncryption(userPwd.getBytes(),ownerPwd.getBytes(),PdfWriter.ALLOW_PRINTING,pdfWriter.ENCRYPTION_AES_128);

            //3.打开文档
            document.open();
            //4.添加一内容段落
            document.add(new Paragraph("hello world")); //添加内容

            Image image1 = Image.getInstance("E:\\workhistory\\springcloud\\UserProvider\\0.jpg");   //添加图片
            image1.setAbsolutePosition(IMAGE_X,IMAGE_Y); //设置图片的x轴和y轴
            image1.scaleAbsolute(200,200);
            document.add(image1);  //添加图片

            Image image2 = Image.getInstance("E:\\workhistory\\springcloud\\UserProvider\\1.jpg");
            document.add(image2);

            //5.关闭文档
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
